"use strict";
const CONFIG = require('../../../../../common/config');
const moment = require('moment');
const async = require('async');
const luaUtil = require('../commonUtils/luaUtil');
const superagent = require('superagent');
const retUtil = require('../commonUtils/retUtil');
const docConvertUtil = require('../commonUtils/docConvertUtil');
const superAgentUtil = require('../commonUtils/superAgentUtil');
const CONSTANT =  require("../../const/CONST");

const productsVideosUtil = (function () {

    function productsVideosUtil() {
    }

    /**
     * 创建视频作品
     * @author mk
     * @date  2018-5-30
     *
     * @param token       必须   检测权限的token
     * @param designId    必须  设计模板id
     * @param content     必须   视频模板对应的内容结构, 的 json 字符串
     * @param callback   参见：https://doc.yijianxiu.com/doc/open_api/api_create_product_video.html
     */
    productsVideosUtil.crateVideoDoc= function (token,designId,content,callback) {
        let tasks = [];
        //1.检测token获取用户userId
        tasks.push(function (callback) {
            luaUtil.getUserId(token,function (err,userId) {
                if (err) {
                    console.log("routes/v1/utils/routerUtils/productsVideosUtil.js typesetVideoDoc err " + err);
                    return callback(CONSTANT.CODE.PERMISSION_DENIED, null);
                }
                let data = {};
                data.userId = userId;
                return callback(null,data);
            });
        });

        //2.获取模板的docString
        tasks.push(function (data,callback) {
            //测试库地址路径
            let url =  CONFIG.API_SERVER_FULL_URL +"/yjx/v1/document/getDocContent_v1_0";
            //本地路
            // let url =  "127.0.0.1:3000/yjx/v1/document/getDocContent_v1_0";
            let param = {
                userId : data.userId,
                id : designId,
                isTmpl : 1
            };
            superAgentUtil.reqRes(url, param, function (err, res){
                if (err) {
                    return callback(CONSTANT.CODE.UNKNOWN_ERR, null);
                }
                data.tmplInfo = res.data.docString;
                return callback(null, data);
            });
        });

        //3.进行转换
        tasks.push(function (data,callback) {
            docConvertUtil.combineApiDocStringWithOpenApiDoc(data.tmplInfo,content,function (err,result) {
                if (err){
                    return callback(CONSTANT.CODE.UNKNOWN_ERR, null);
                }
                data.docString = result;
                return callback(null,data);
            });
        });
        //4.创建作品
        tasks.push(function (data,callback) {
            //测试库地址路径
            let url =  CONFIG.API_SERVER_FULL_URL +"/yjx/v1/document/newUserDoc_v1_0";
            //本地路径
            // let url =  "127.0.0.1:3000/yjx/v1/document/newUserDoc_v1_0";
            let param = {
                userId : data.userId,
                tmplId : designId,
                docString : data.docString
            };
            superAgentUtil.reqRes(url, param, function (err, res){
                if (err) {
                    return callback(CONSTANT.CODE.UNKNOWN_ERR, null);
                }
                data.product_id = res.data.docId;
                return callback(null, data);
            });
        });
        async.waterfall(tasks, function (error, data) {
            if(error) return callback(error, null);
            return callback(error, {product_id : data.product_id});
        });
    };


    /**
     * 修改视频作品
     * @author mk
     * @date  2018-5-30
     *
     * @param token       必须   检测权限的token
     * @param productId   必须  视频模板唯一标识
     * @param content     必须  视频模板对应的内容结构, 的 json 字符串
     * @param callback   参见： https://doc.yijianxiu.com/doc/open_api/api_update_product_video.html
     */
    productsVideosUtil.updateVideoDoc= function (token,productId,content,callback) {
        let tasks = [];
        //1.获取userId
        tasks.push(function (callback) {
            luaUtil.getUserId(token,function (err,userId) {
                if (err) {
                    console.log("routes/v1/utils/routerUtils/productsVideosUtil.js typesetVideoDoc err " + err);
                    return callback(CONSTANT.CODE.PERMISSION_DENIED, null);
                }
                let data = {};
                data.userId = userId;
                return callback(null,data);
            });
        });
        //2.获取作品原数据
        tasks.push(function (data,callback) {
            //测试库地址路径
            let url =  CONFIG.API_SERVER_FULL_URL +"/yjx/v1/document/getDocContent_v1_0";
            //本地路
            // let url =  "127.0.0.1:3000/yjx/v1/document/getDocContent_v1_0";
            let param = {
                userId : data.userId,
                id : productId,
                isTmpl : 0
            };
            superAgentUtil.reqRes(url, param, function (err, res){
                if (err) {
                    return callback(CONSTANT.CODE.UNKNOWN_ERR, null);
                }
                data.userTmplInfo = res.data.docString;
                return callback(null, data);
            });
        });
        //3.转换
        tasks.push(function (data,callback) {
            docConvertUtil.combineApiDocStringWithOpenApiDoc(data.userTmplInfo,content,function (err,result) {
                if (err){
                    return callback(CONSTANT.CODE.UNKNOWN_ERR, null);
                }
                data.docString = result;
                return callback(null,data);
            });
        });
        //4.更新
        tasks.push(function (data,callback) {
            //测试库地址路径
            let url =  CONFIG.API_SERVER_FULL_URL +"/yjx/v1/document/updateUserDoc_v1_0";
            //本地路径
            // let url =  "127.0.0.1:3000/yjx/v1/document/updateUserDoc_v1_0";
            let param = {
                userId : data.userId,
                docId : productId,
                docString : data.docString
            };

            superAgentUtil.reqRes(url, param, function (err, res){
                if (err) {
                    return callback(CONSTANT.CODE.UNKNOWN_ERR, null);
                }
                data.product_id = res.data.docId;
                return callback(null, data);
            });
        });
        async.waterfall(tasks, function (error, data) {
            if(error) return callback(error, null);
            return callback(null, {});
        });

    };

    /**
     * 获取视频作品信息
     * @author mk
     * @date  2018-5-30
     *
     * @param token        必须   检测权限的token
     * @param productId    必须  视频模板唯一标识
     * @param callback   参见： https://doc.yijianxiu.com/doc/open_api/api_product_video_content.html
     */
    productsVideosUtil.getVideoDocContent= function (token,productId,callback) {
        let tasks = [];
        //1.获取userId
        tasks.push(function (callback) {
            luaUtil.getUserId(token,function (err,userId) {
                if (err) {
                    console.log("routes/v1/utils/routerUtils/productsVideosUtil.js typesetVideoDoc err " + err);
                    return callback(CONSTANT.CODE.PERMISSION_DENIED, null);
                }
                let data = {};
                data.userId = userId;
                return callback(null,data);
            });
        });
        //获取作品的核心数据
        tasks.push(function (data,callback) {
            //测试库地址路径
            let url =  CONFIG.API_SERVER_FULL_URL +"/yjx/v1/document/getDocContent_v1_0";
            //本地路
            // let url =  "127.0.0.1:3000/yjx/v1/document/getDocContent_v1_0";
            let param = {
                userId : data.userId,
                id : productId,
                isTmpl : 0
            };
            //像api请求模板数据
            superAgentUtil.reqRes(url, param, function (err, res){
                if (err) {
                    return callback(CONSTANT.CODE.UNKNOWN_ERR, null);
                }
                data.userTmplInfo = res.data.docString ||[];
                return callback(null, data);
            });
        });
        //3.转换数据
        tasks.push(function (data,callback) {
            docConvertUtil.covertApiDocStringToOpenApiDoc( data.userTmplInfo,function (err,content) {
                if (err){
                    return callback(CONSTANT.CODE.UNKNOWN_ERR, null);
                }
                data.content = content || [];
                return callback(null,data);
            });
        });
        async.waterfall(tasks, function (error, data) {
            if(error) return callback(error, null);
            return callback(error, {product_id:productId,content: data.content});
        });

    };


    /**
     * 获取视频作品信息
     * @author mk
     * @date  2018-5-30
     *
     * @param token        必须   获取用户id的token
     * @param productId    必须  视频模板唯一标识
     * @param callback   参见： https://doc.yijianxiu.com/doc/open_api/api_product_video_preview.html
     */
    productsVideosUtil.typesetVideoDoc= function (token,productId,callback) {
        luaUtil.getUserId(token,function (err,userId) {
            if (err) {
                console.log("routes/v1/utils/routerUtils/productsVideosUtil.js typesetVideoDoc err " + err);
                return callback(CONSTANT.CODE.UNKNOWN_ERR, null);
            }
            //测试库地址路径
            let url =  CONFIG.API_SERVER_FULL_URL +"/yjx/v1/document/requestUserDocPrivewInfo_web_v1_1";
            //本地路径
            // let url =  "127.0.0.1:3000/yjx/v1/document/requestUserDocPrivewInfo_web_v1_1";
            let param = {
                userId : userId,
                docId : productId,
                wait : 0
            };
            superAgentUtil.reqRes(url, param, function (err, res){
                if (err) {
                    return callback(CONSTANT.CODE.UNKNOWN_ERR, null);
                }
                let data = {
                    id : res.data.id,
                    status : res.data.docInfo.generateStatus,
                };
                if (data.status === 3 && res.data.previewInfo.video) {
                    let videoInfo = {
                        thumb:res.data.previewInfo.video.thumbs[0] || "",
                        video:res.data.previewInfo.video.videoUrl || "",
                        duration:res.data.previewInfo.video.duration || 0,
                        video_size:res.data.previewInfo.video.videoSize || 0,
                        w:res.data.previewInfo.video.w || 0,
                        h:res.data.previewInfo.video.h || 0
                    };
                    data.video_info = videoInfo;
                    return callback(null, data);
                }else if (data.status === 1 && res.data.progressInfo){
                    let progressInfo = {
                        progress:res.data.progressInfo.progress,
                        next_interval : res.data.progressInfo.nextInterval
                    };
                    data.progress_info = progressInfo;
                    return callback(null, data);
                } else{
                    return callback(null, data);
                }
            });
        });
    };
    return productsVideosUtil;
})();

module.exports = productsVideosUtil;